<template>
  <div class="upload-img">
    <el-upload
      class="upload"
      action="#"
      :show-file-list="false"
      :http-request="httpRequest"
      :before-upload="beforeUpload"
    >
      <img v-if="value" v-imgerror :src="value" class="avatar">
      <i v-else class="el-icon-plus" />
    </el-upload>
    <!-- 删除图标 -->
    <i v-if="value" class="el-icon-delete del" @click="$emit('input', '')" />
    <!-- 放大按钮 -->
    <i v-if="value" class="el-icon-zoom-in big" @click="isShow=true" />
    <!-- 图片预览弹框 -->
    <el-dialog title="图片预览" :visible.sync="isShow">
      <img v-imgerror style="width:100%;" :src="value" alt="">
    </el-dialog>
    <!-- 进度条 -->
    <el-progress v-if="showProgress" :width="180" type="circle" :percentage="percentage" class="progress" />
  </div>

</template>
<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDNXR8bpeSF1JL8EyZopxRfdHgqGUeuEn8',
  SecretKey: 'HJ9mAbQEn7jgjY9swEI6yAamk7MN8hty'
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showProgress: false,
      percentage: 0,
      imageUrl: '',
      isShow: false
    }
  },
  methods: {
    beforeUpload(file) {
      // 只能上传png与jpg
      const limitType = file.type === 'image/jpeg' || file.type === 'image/png'
      if (!limitType) {
        this.$message.error('请上传jpg或者png图片')
      }
      return limitType
    },
    httpRequest(res) {
      const file = res.file
      this.showProgress = true
      cos.uploadFile({
        Bucket: 'dhf81-1305249343', /* 存储桶，必须字段 */
        Region: 'ap-guangzhou', /* 存储桶所在地域，必须字段 */
        Key: file.name, /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */
        Body: file, // 上传文件对象
        SliceSize: 1024 * 1024 * 0.5, /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
        onProgress: (progressData) => {
          this.percentage = +(progressData.percent * 100).toFixed(2)
          if (progressData.percent === 1) {
            this.showProgress = false
          }
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        if (err) {
          console.log('上传失败', err)
        } else {
          this.$emit('input', 'http://' + data.Location)
          //   this.imageUrl = 'http://' + data.Location
          console.log('上传成功', data)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.upload{
    width:180px;
    height:180px;
    border:2px dashed #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
    /*
    绝对定位 + left:50%  right:50%; transform:
    flex
    绝对定位+margin:auto auto  left 0right: 0  top:0  bottom:0
    */
    &:hover{
     border:2px dashed #5eadff;
    }
    .el-icon-plus{
        font-size: 28px;
    }
    .avatar{
        width:100%;
        height:100%;
    }
}
.upload-img{
      width:180px;
  position: relative;
  &:hover{
    .del,.big{
      display: block;
    }
  }
  .del,.big{
    position: absolute;
    top:5px;
    right:5px;
    cursor: pointer;
    display: none;
    &:hover{
      color:red;
    }
  }
  .big{
    right:35px;
  }
}
.progress{
  position: absolute;
  left:0;
  top:0;
  background-color: #fff;
}
</style>
